package no.group.gowther.enums;

import no.group.gowther.util.FileUtils;

import java.nio.file.Path;
import java.util.Set;

/**
 * @author ayatsuji
 * @date 2022-04-11 23:40
 * @since
 */
public enum FileType {
    ALL {
        @Override
        public boolean isMyType(Path filePath) {
            return true;
        }
    },
    IMAGE {
        private final Set<String> imageFileExtendName = Set.of("jpg", "jpeg", "png", "bmp", "webp", "svg", "gif");

        @Override
        public boolean isMyType(Path filePath) {
            return checkByFilenameExtension(filePath, imageFileExtendName);
        }
    },
    DIR {
        @Override
        public boolean isMyType(Path filePath) {
            return filePath.toFile().isDirectory();
        }
    },
    VIDEO {
        private final Set<String> videoFileExtendName = Set.of();

        @Override
        public boolean isMyType(Path filePath) {
            return checkByFilenameExtension(filePath, videoFileExtendName);
        }
    },
    COMPRESS {
        private final Set<String> compressFileExtendName = Set.of("zip", "rar", "7z");

        @Override
        public boolean isMyType(Path filePath) {
            return checkByFilenameExtension(filePath, compressFileExtendName);
        }
    },
    OTHER {
        @Override
        public boolean isMyType(Path filePath) {
            return true;
        }
    }
    ;

    private static final FileType[] SHOULD_RESOLVE_TYPES = {IMAGE, DIR};

    public static FileType resolveType(Path filePath) {
        for (FileType type: SHOULD_RESOLVE_TYPES) {
            if (!type.isMyType(filePath)) continue;
            return type;
        }
        return OTHER;
    }

    public abstract boolean isMyType(Path filePath);

    protected boolean checkByFilenameExtension(Path filePath, Set<String> extendName) {
        return extendName.contains(FileUtils.extractExtension(filePath.toString()));
    }

    public String getType() {
        return toString();
    }
}
